package be.fw.mana;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JOptionPane;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class ManaWriter
{
    private final static int TEAMNAME_LENGTH = 23;
    private final static int PLAYERNAME_LENGTH = 26;
    private final static int PLAYERS_PER_TEAM = 20;
    private final static int NUMBER_OF_TEAMS = 64;
    private final static int NUMBER_OF_EUROPEAN_TEAMS = 136;

    /**
     * Constructor
     */
    public ManaWriter()
    {
        try
        {
            FileInputStream fis = new FileInputStream("MANA.DAT");
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("MANA_NEW.DAT"));
            Workbook workbook = Workbook.getWorkbook(new File("mana.xls"));
            verwerkLeague(workbook, bos, fis);
            verwerkEuropa(workbook, bos, fis);
            bos.close();
            fis.close();
            workbook.close();
            System.out.println("done");
            JOptionPane.showMessageDialog(null, "Reading of mana.xls completed.\n\nA MANA_NEW.DAT file has been written in the same directory as this tool.\nIf you want to use this file in BMP rename it to MANA.DAT (make a backup of the original MANA.DAT !!!)", "Import OK :-)", JOptionPane.INFORMATION_MESSAGE);
        }
        catch (BiffException e)
        {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "Export failed.\n\nBe sure to place this tool in your BMP/TM directory where MANA.DAT resides and that a mana.xls file is available in this directory.", "Import FAILED :-(", JOptionPane.ERROR_MESSAGE);
        }
        catch (IOException e)
        {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "Export failed.\n\nBe sure to place this tool in your BMP/TM directory where MANA.DAT resides and that a mana.xls file is available in this directory.", "Import FAILED :-(", JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * Schrijven europese gegevens
     * @param workbook
     * @param bos
     * @param fis
     * @throws IOException
     */
    private void verwerkEuropa(Workbook workbook, BufferedOutputStream bos, FileInputStream fis) throws IOException
    {
        Sheet sheet = workbook.getSheet("Europe");
        for (int i = 0; i < NUMBER_OF_EUROPEAN_TEAMS; i++)
        {
            String teamNaam = sheet.getCell(0, i).getContents().trim().toUpperCase();
            System.out.println("working on : " + teamNaam);
            // kopieren van de bytes van de teamnaam naar een correcte lengte
            // byte array
            byte[] excelBytesTeamNaam = teamNaam.getBytes();
            byte[] writeBytesTeamNaam = new byte[TEAMNAME_LENGTH];
            for (int j = 0; j < writeBytesTeamNaam.length; j++)
            {
                if ((j < excelBytesTeamNaam.length) && (j < TEAMNAME_LENGTH - 2))
                    writeBytesTeamNaam[j] = excelBytesTeamNaam[j];
            }
            // effectief schrijven teamnaam
            bos.write(writeBytesTeamNaam);
        }
    }

    /**
     * Verwerken leagueploegen
     *
     * @param workbook2
     * @param bos2
     * @param fis
     * @throws IOException
     */
    private void verwerkLeague(Workbook workbook, BufferedOutputStream bos, FileInputStream fis) throws IOException
    {
        // eerste 984 bytes kopieren
        for (int i = 0; i < 984; i++)
        {
            bos.write(fis.read());
        }

        for (int i = 0; i < NUMBER_OF_TEAMS; i++)
        {
            Sheet sheet = workbook.getSheet(i);
            String teamNaam = sheet.getCell(0, 0).getContents().trim().toUpperCase();
            System.out.println("working on : " + teamNaam);
            // kopieren van de bytes van de teamnaam naar een correcte lengte
            // byte array
            byte[] excelBytesTeamNaam = teamNaam.getBytes();
            byte[] writeBytesTeamNaam = new byte[TEAMNAME_LENGTH];
            for (int j = 0; j < writeBytesTeamNaam.length; j++)
            {
                if ((j < excelBytesTeamNaam.length) && (j < TEAMNAME_LENGTH - 2))
                    writeBytesTeamNaam[j] = excelBytesTeamNaam[j];
            }
            // effectief schrijven teamnaam
            bos.write(writeBytesTeamNaam);

            for (int j = 0; j < PLAYERS_PER_TEAM; j++)
            {
                String spelerNaam = "";
                if(sheet.getRows() > (j+2)) spelerNaam = sheet.getCell(0, j + 2).getContents().trim().toUpperCase();
                System.out.println("   " + spelerNaam);
                // kopieren van de bytes van de spelernaam naar een correcte
                // lengte byte array
                byte[] excelBytesSpelerNaam = spelerNaam.getBytes();
                byte[] writeBytesSpelerNaam = new byte[PLAYERNAME_LENGTH];
                for (int k = 0; k < writeBytesSpelerNaam.length; k++)
                {
                    if ((k < excelBytesSpelerNaam.length) && (k < PLAYERNAME_LENGTH / 2))
                        writeBytesSpelerNaam[k] = excelBytesSpelerNaam[k];
                }
                // effectief schrijven spelernaam
                bos.write(writeBytesSpelerNaam);
            }
        }
    }
}
